package cl.jpavlich.mpm.model;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.metawidget.inspector.annotation.UiComesAfter;
import org.metawidget.inspector.annotation.UiHidden;

public class ModPackage implements Comparable<ModPackage> {
	private String name;
	
	private String author;
	
	private String description;
	
	private URI homepage;
	
	private String modversion;
	
	private String minecraftversion;
	
	private URI downloadURL;
	
	private String localFilename;
	
	private boolean requiresLocalInstallation;
	
	private Set<ModPackage> dependencies = new HashSet<ModPackage>();
	
	private List<Command> installation = new ArrayList<Command>();
	

	public ModPackage() {
		
	}
	
	public int compareTo(ModPackage o) {
		if (name != null && o.name != null) {
			return name.compareTo(o.name);
		} else { 
			return hashCode() - o.hashCode();
		}
	}
	


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@UiComesAfter("homepage")
	public String getModversion() {
		return modversion;
	}

	public void setModversion(String version) {
		this.modversion = version;
	}

	@UiComesAfter("modversion")
	public String getMinecraftversion() {
		return minecraftversion;
	}

	public void setMinecraftversion(String minecraftVersion) {
		this.minecraftversion = minecraftVersion;
	}

	@UiComesAfter("name")
	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}


	@UiComesAfter("description")
	public URI getHomepage() {
		return homepage;
	}

	public void setHomepage(URI url) {
		this.homepage = url;
	}

	@UiComesAfter("author")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@UiHidden
	public List<Command> getInstallation() {
		return installation;
	}

	@UiComesAfter("description")
	public URI getDownloadURL() {
		return downloadURL;
	}

	public void setDownloadURL(URI location) {
		this.downloadURL = location;
	}

	
	public void install() throws Exception {
		for (Command c : installation) {
			c.execute();
		}
	}

	public boolean addCommand(Command cmd) {
		return installation.add(cmd);
	}

	public boolean removeCommand(Object cmd) {
		return installation.remove(cmd);
	}
	
	public boolean addDependency(ModPackage dep) {
		return dependencies.add(dep);
	}
	
	public boolean removeDependency(ModPackage dep) {
		return dependencies.remove(dep);
	}

	@UiComesAfter("requiresLocalInstallation")
	public String getLocalFilename() {
		return localFilename;
	}

	public void setLocalFilename(String localFilename) {
		this.localFilename = localFilename;
	}

	@UiComesAfter("downloadURL")
	public boolean isRequiresLocalInstallation() {
		return requiresLocalInstallation;
	}

	public void setRequiresLocalInstallation(boolean requireLocalInstallation) {
		this.requiresLocalInstallation = requireLocalInstallation;
	}



}
